﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using System.Data;
using System.Data.Linq;

namespace BusinessAccessLayer
{
    public class BAChiTietHoaDon
    {
        QuanLyBanHangDataContext dbs = new QuanLyBanHangDataContext();

        public List<ChiTietHoaDon> getTable2()
        {
            return dbs.ChiTietHoaDons.ToList();
        }
        public DataTable getTable()
        {
            DataTable cthd=new DataTable();

            cthd.Columns.Add("MaHD", typeof(string));
            cthd.Columns.Add("MaSP", typeof(string));
            cthd.Columns.Add("SoLuong", typeof(string));
         
            foreach (var item in dbs.ChiTietHoaDons.ToList())
            {
                DataRow row = cthd.NewRow();
                row["MaHD"] = item.MaHD;
                row["MaSP"] = item.MaSP;
                row["SoLuong"] = item.SoLuong;
                cthd.Rows.Add(row);
            }
            return cthd;
        }
        public bool ThemCTHD(string strMaHD, string strMaSP, int strSoLuong)
        {
            bool f = false;
            try
            {
                ChiTietHoaDon cthd = new ChiTietHoaDon()
                {
                    MaHD = strMaHD,
                    MaSP = strMaSP,
                   // SoLuong = int.Parse(strSoLuong)
                   SoLuong=strSoLuong

                };
                dbs.ChiTietHoaDons.InsertOnSubmit(cthd);
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;
        }
        public bool XoaCTHD(string strMaHD, string strMaSP)
        {
            bool f = false;
            try
            {
                ChiTietHoaDon chitiet = dbs.ChiTietHoaDons.Single(cthd => cthd.MaHD == strMaHD && cthd.MaSP == strMaSP);
                dbs.ChiTietHoaDons.DeleteOnSubmit(chitiet);
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;
        }
        public bool CapNhatCTHD(string strMaHD, string strMaSP, int strSoLuong)
        {
            bool f = false;
            try
            {
                ChiTietHoaDon chitiet =
                    dbs.ChiTietHoaDons.
                    Single(cthd => cthd.MaHD == strMaHD && cthd.MaSP == strMaSP);
               // chitiet.SoLuong = int.Parse(strSoLuong);
                chitiet.SoLuong =strSoLuong;
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;

        }
    }
}

